Ext.define('SKJF.controller.PO',{
	extend: 'Ext.app.Controller',
	views: ['PEMBELIAN.v_po','PEMBELIAN.v_po_form'],
	models: ['m_po'],
	stores: ['s_po'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listpo',
		selector: 'Listpo'
	}, {
		ref: 'Listpodetail',
		selector: 'Listpodetail'
	},	{
		ref: 'v_po_form',
		selector: 'v_po_form'
	}, {
		ref: 'Savepo',
		selector: 'v_po_form #save'
	}, {
		ref: 'Createpo',
		selector: 'v_po_form #create'
	}, {
		ref: 'PO',
		selector: 'PO #center'
	}],


	init: function(){
		this.control({
			'PO': {
				'afterrender': this.poAfterRender
			},
			'v_po_form': {
				'afterlayout': this.poAfterLayout
			},
			'Listpo': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListpo
			},
			'Listpo button[action=create]': {
				click: this.createRecord
			},
			'Listpo button[action=delete]': {
				click: this.deleteRecord
			},
			'Listpo button[action=print]': {
				click: this.printRecords
			},
			'v_po_form button[action=save]': {
				click: this.saveV_po_form
			},
			'v_po_form button[action=create]': {
				click: this.saveV_po_form
			},
			'v_po_form button[action=cancel]': {
				click: this.cancelV_po_form
			}
		});
	},
	
	poAfterRender: function(){
		var poStore = this.getListpo().getStore();
		poStore.load();
	},
	
	poAfterLayout: function(){this.getV_po_form().down('#NAMA_SUPPLIER_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListpo	= this.getListpo();
		var getV_po_form= this.getV_po_form(),
			form			= getV_po_form.getForm();
		var getSavepo	= this.getSavepo();
		var getCreatepo	= this.getCreatepo();
		
		/* grid-panel */
		getListpo.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_po_form.down('#NOMOR_PO_field').setReadOnly(true);
		getSavepo.setDisabled(true);
		getCreatepo.setDisabled(false);
		getV_po_form.setDisabled(false);
		
		this.getPO().setActiveTab(getV_po_form);		
	},
	
	enableDelete: function(dataview, selections){
		if (selections.length) {
			var sel = selections[0].data;
			// console.info('Sel : '+sel.NOMOR_DO);
			this.getListpo().down('#btndelete').setDisabled(!selections.length);
			this.getListpodetail().down('#btncreate').setDisabled(false);
			this.getListpodetail().down('#btnprint').setDisabled(false);
			
			this.getListpodetail().getStore().load({
				params: {
					NOMOR_PO: sel.NOMOR_PO
				}
			});
			
		}else{
			this.getListpo().down('#btndelete').setDisabled(!selections.length);
			this.getListpodetail().down('#btncreate').setDisabled(true);
			this.getListpodetail().down('#btndelete').setDisabled(true);
			this.getListpodetail().down('#btnprint').setDisabled(true);
			this.getListpodetail().getStore().removeAll();
		}
		
	},
	
	updateListpo: function(me, record, item, index, e){
		var getPO		= this.getPO();
		var getListpo	= this.getListpo();
		var getV_po_form= this.getV_po_form(),
			form			= getV_po_form.getForm();
		var getSavepo	= this.getSavepo();
		var getCreatepo	= this.getCreatepo();
		
		getSavepo.setDisabled(false);
		getCreatepo.setDisabled(true);
		getV_po_form.down('#NOMOR_PO_field').setReadOnly(true);		
		getV_po_form.loadRecord(record);
		
		getListpo.setDisabled(true);
		getV_po_form.setDisabled(false);
		getPO.setActiveTab(getV_po_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListpo().getStore();
		var selection = this.getListpo().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "NOMOR_PO" = "'+selection.data.NOMOR_PO+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListpo().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_po/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/po.html','po_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_po_form: function(){
		var getPO		= this.getPO();
		var getListpo 	= this.getListpo();
		var getV_po_form= this.getV_po_form(),
			form			= getV_po_form.getForm(),
			values			= getV_po_form.getValues();
		var store 			= this.getStore('s_po');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_po/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('NOMOR_PO') === values.NOMOR_PO) {
										return true;
									}
									return false;
								}
							);
							/* getListpo.getView().select(recordIndex); */
							getListpo.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_po_form.setDisabled(true);
					getListpo.setDisabled(false);
					getPO.setActiveTab(getListpo);
				}
			});
		}
	},
	
	createV_po_form: function(){
		var getPO		= this.getPO();
		var getListpo 	= this.getListpo();
		var getV_po_form= this.getV_po_form(),
			form			= getV_po_form.getForm(),
			values			= getV_po_form.getValues();
		var store 			= this.getStore('s_po');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_po/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_po_form.setDisabled(true);
					getListpo.setDisabled(false);
					getPO.setActiveTab(getListpo);
				}
			});
		}
	},
	
	cancelV_po_form: function(){
		var getPO		= this.getPO();
		var getListpo	= this.getListpo();
		var getV_po_form= this.getV_po_form(),
			form			= getV_po_form.getForm();
			
		form.reset();
		getV_po_form.setDisabled(true);
		getListpo.setDisabled(false);
		getPO.setActiveTab(getListpo);
	}
	
});